﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.SessionState;
using AccessDAL;
using AccessModel;

namespace web.ajax
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ajaxhandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                context.Response.ContentType = "application/json";

                var methodname = context.Request.Params["methodname"];
                if (!string.IsNullOrEmpty(methodname))
                {
                    switch (methodname)
                    {
                        case "AddMenu":
                            string menuName = context.Request.Params["menuName"];
                            string menuLink = context.Request.Params["menuLink"];
                            string menuTarget = context.Request.Params["menuTarget"];

                            context.Response.Write(CreateMenu(menuName, menuLink, menuTarget));
                            return;
                        case "checkUserLogin":
                            string userName = context.Request.Params["userName"];
                            string passWord = context.Request.Params["passWord"];

                            context.Response.Write(checkUserLogin(userName, passWord));
                            return;

                        case "checkUserIsExits":
                            string txtUserName = context.Request.Params["txtUserName"];
                            context.Response.Write(checkUserIsExits(txtUserName));
                            return;
                    }

                }
                else
                {
                    context.Response.Write("");
                }
            }
            catch (Exception ex)
            {

            }
        }

        private int CreateMenu(string _menuName, string _menuLink, string _menuTarget)
        {
            MenuDAL md = new MenuDAL();
            Menu m = new Menu();
            m.menudate = DateTime.Now;
            m.menuname = _menuName;
            m.menulink = _menuLink;
            m.menutarget = _menuTarget;
            int i = md.createUser(m);

            return i;
        }


        private string checkUserLogin(string _userName, string _passWord)
        {
            Employees employee = EmployeesDAL.CheckUserInfo(_userName, _passWord);
            if (employee.id != 0)
            {
                return "{vali:\"1\"}";
            }
            else
            {
                return "{vali:\"0\"}";
            }
        }

        [WebMethod]
        public bool checkUserIsExits(string _userName)
        {
            if (EmployeesDAL.CheckUserIsExits(_userName))
            {
                return true;
            }
            return false;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


    }
}
